package com.google.android.material.circularreveal;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import o.asf;

public class CircularRevealHelper {
    public static final int ˊ;
    @Nullable
    private o.arm.d ʻ;
    @Nullable
    private Drawable ʼ;
    private boolean ʽ;
    private final Paint ˋ = new Paint(7);
    private final View ˎ;
    private final Path ˏ = new Path();
    private final d ॱ;
    private final Paint ॱॱ = new Paint(1);
    private boolean ᐝ;

    public interface d {
        void ˎ(Canvas canvas);

        boolean ˏ();
    }

    @Retention(RetentionPolicy.SOURCE)
    public @interface Strategy {
    }

    static {
        if (VERSION.SDK_INT >= 21) {
            ˊ = 2;
        } else if (VERSION.SDK_INT >= 18) {
            ˊ = 1;
        } else {
            ˊ = 0;
        }
    }

    public CircularRevealHelper(d dVar) {
        this.ॱ = dVar;
        this.ˎ = (View) dVar;
        this.ˎ.setWillNotDraw(false);
        this.ॱॱ.setColor(0);
    }

    public void ˋ() {
        if (ˊ == 0) {
            this.ᐝ = true;
            this.ʽ = false;
            this.ˎ.buildDrawingCache();
            Bitmap drawingCache = this.ˎ.getDrawingCache();
            if (!(drawingCache != null || this.ˎ.getWidth() == 0 || this.ˎ.getHeight() == 0)) {
                drawingCache = Bitmap.createBitmap(this.ˎ.getWidth(), this.ˎ.getHeight(), Config.ARGB_8888);
                this.ˎ.draw(new Canvas(drawingCache));
            }
            if (drawingCache != null) {
                this.ˋ.setShader(new BitmapShader(drawingCache, TileMode.CLAMP, TileMode.CLAMP));
            }
            this.ᐝ = false;
            this.ʽ = true;
        }
    }

    public void ˎ() {
        if (ˊ == 0) {
            this.ʽ = false;
            this.ˎ.destroyDrawingCache();
            this.ˋ.setShader(null);
            this.ˎ.invalidate();
        }
    }

    public void ॱ(@Nullable o.arm.d dVar) {
        if (dVar == null) {
            this.ʻ = null;
        } else {
            if (this.ʻ == null) {
                this.ʻ = new o.arm.d(dVar);
            } else {
                this.ʻ.ॱ(dVar);
            }
            if (asf.ˊ(dVar.ˎ, ˎ(dVar), 1.0E-4f)) {
                this.ʻ.ˎ = Float.MAX_VALUE;
            }
        }
        ᐝ();
    }

    @Nullable
    public o.arm.d ˏ() {
        if (this.ʻ == null) {
            return null;
        }
        o.arm.d dVar = new o.arm.d(this.ʻ);
        if (!dVar.ˊ()) {
            return dVar;
        }
        dVar.ˎ = ˎ(dVar);
        return dVar;
    }

    public void ˋ(@ColorInt int i) {
        this.ॱॱ.setColor(i);
        this.ˎ.invalidate();
    }

    @ColorInt
    public int ˊ() {
        return this.ॱॱ.getColor();
    }

    public void ˋ(@Nullable Drawable drawable) {
        this.ʼ = drawable;
        this.ˎ.invalidate();
    }

    private void ᐝ() {
        if (ˊ == 1) {
            this.ˏ.rewind();
            if (this.ʻ != null) {
                this.ˏ.addCircle(this.ʻ.ˊ, this.ʻ.ˏ, this.ʻ.ˎ, Direction.CW);
            }
        }
        this.ˎ.invalidate();
    }

    private float ˎ(o.arm.d dVar) {
        return asf.ˎ(dVar.ˊ, dVar.ˏ, 0.0f, 0.0f, (float) this.ˎ.getWidth(), (float) this.ˎ.getHeight());
    }

    public void ˎ(Canvas canvas) {
        if (ʼ()) {
            switch (ˊ) {
                case 0:
                    canvas.drawCircle(this.ʻ.ˊ, this.ʻ.ˏ, this.ʻ.ˎ, this.ˋ);
                    if (ʻ()) {
                        canvas.drawCircle(this.ʻ.ˊ, this.ʻ.ˏ, this.ʻ.ˎ, this.ॱॱ);
                        break;
                    }
                    break;
                case 1:
                    int save = canvas.save();
                    canvas.clipPath(this.ˏ);
                    this.ॱ.ˎ(canvas);
                    if (ʻ()) {
                        canvas.drawRect(0.0f, 0.0f, (float) this.ˎ.getWidth(), (float) this.ˎ.getHeight(), this.ॱॱ);
                    }
                    canvas.restoreToCount(save);
                    break;
                case 2:
                    this.ॱ.ˎ(canvas);
                    if (ʻ()) {
                        canvas.drawRect(0.0f, 0.0f, (float) this.ˎ.getWidth(), (float) this.ˎ.getHeight(), this.ॱॱ);
                        break;
                    }
                    break;
                default:
                    throw new IllegalStateException("Unsupported strategy " + ˊ);
            }
        }
        this.ॱ.ˎ(canvas);
        if (ʻ()) {
            canvas.drawRect(0.0f, 0.0f, (float) this.ˎ.getWidth(), (float) this.ˎ.getHeight(), this.ॱॱ);
        }
        ॱ(canvas);
    }

    private void ॱ(Canvas canvas) {
        if (ॱॱ()) {
            Rect bounds = this.ʼ.getBounds();
            float width = this.ʻ.ˊ - (((float) bounds.width()) / 2.0f);
            float height = this.ʻ.ˏ - (((float) bounds.height()) / 2.0f);
            canvas.translate(width, height);
            this.ʼ.draw(canvas);
            canvas.translate(-width, -height);
        }
    }

    public boolean ॱ() {
        return this.ॱ.ˏ() && !ʼ();
    }

    private boolean ʼ() {
        boolean z;
        if (this.ʻ == null || this.ʻ.ˊ()) {
            z = true;
        } else {
            z = false;
        }
        if (ˊ == 0) {
            return !z && this.ʽ;
        } else {
            if (z) {
                return false;
            }
            return true;
        }
    }

    private boolean ʻ() {
        return (this.ᐝ || Color.alpha(this.ॱॱ.getColor()) == 0) ? false : true;
    }

    private boolean ॱॱ() {
        return (this.ᐝ || this.ʼ == null || this.ʻ == null) ? false : true;
    }
}
